package com.example.doer.reading.AppActivities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.view.sliding.AbSlidingPlayView;
import com.example.doer.reading.AppAdapters.categorysAdapter;
import com.example.doer.reading.AppDataModel.Theme;
import com.example.doer.reading.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CategorysActivity extends AppCompatActivity {

    AbSlidingPlayView abSlidingPlayView = null;
    private GridView gvCategory;
    private View view;

    private List<Theme> categoryList=new ArrayList<Theme>();
    private categorysAdapter categorysAdapter;

    private Toolbar toolbar;
    AbHttpUtil abHttpUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categorys);
        gvCategory = (GridView) findViewById(R.id.gv_categorys);

        toolbar= (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("分类");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.return_button);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        abHttpUtil= AbHttpUtil.getInstance(this);
        String url="http://192.168.191.1/Reading/BookStoreServlet?Action=Categorys";
        abHttpUtil.get(url, new AbStringHttpResponseListener() {
            @Override
            public void onSuccess(int i, String s) {
                try {
                    JSONObject jsonObject=new JSONObject(s);
                    JSONArray jsonArray=jsonObject.getJSONArray("Categorys");
                    for (int j=0;j<jsonArray.length();j++){
                        JSONObject object=jsonArray.getJSONObject(j);
                        Theme theme=new Theme();
                        theme.setThemeID(object.getString("TopicID"));
                        theme.setThemeName(object.getString("TopicName"));
                        theme.setThemeDescription(object.getString("TopicDescription"));
                        theme.setThemeFocusNum(object.getInt("TopicFocusNum"));
                        theme.setQuestionNumOfTheme(object.getInt("questionCount"));
                        categoryList.add(theme);
                    }
                    //Gson gson=new Gson();
                    //categoryList=gson.fromJson(jsonArray.toString(),new TypeToken<List<Theme>>(){}.getType());
                    categorysAdapter = new categorysAdapter(CategorysActivity.this,categoryList);
                    gvCategory.setAdapter(categorysAdapter);
                    Log.e("json:",s);

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onStart() {

            }
            @Override
            public void onFinish() {

            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {

            }

        });

        gvCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(CategorysActivity.this,OtherBooks.class);
                intent.putExtra("Category",categoryList.get(i).getThemeName());
                startActivity(intent);
            }
        });

    }
}
